import { request, baseURL, type Response } from "@/utils/request";
import { type AxiosRequestConfig } from "axios";

let { s } = window as any;
export default {
    // 上传文件
    uploadFile(data: { file: File }, onUploadProgress: AxiosRequestConfig["onUploadProgress"]) {
        let formData = new FormData();
        for(let key in data) {
            formData.append(key, data[key]);
        }
        return request({
            url: "/upload/uploadFile",
            data: formData,
            method: "post",
            onUploadProgress,
        }).then((res) => res.data as Response<{ fileUrl: string }>);
    },
    // 上传切片
    uploadChunk(data:{ hash: string, chunkIndex: string, chunkFile: Blob}) {
        let formData = new FormData();
        for(let key in data) {
            formData.append(key, data[key]);
        }
        return request({
            url: "/upload/uploadChunk",
            data: formData,
            method: "post",
        }).then((res) => res.data as Response);
    },
    // 合并切片
    mergeChunk(data: { hash: string; suffix: string }) {
        return request({
            url: "/upload/mergeChunk",
            data: s(data),
            method: "post",
        }).then((res) => res.data as Response<{ fileUrl: string }>);
    },
    // 检查切片
    checkChunk(data: { hash: string; suffix: string }) {
        return request({
            url: "/upload/checkChunk",
            data: s(data),
            method: "post",
        }).then((res) => res.data as Response<{ fileUrl: string; chunkIndexList: number[] }>);
    },
};
